#include "Comm.h"
#include "string.h"
#include <vector>

void Comm::ClientRecvr(void* threadParams) {
	portIPSim* args = (portIPSim*)threadParams;
	char Buffer[10];
	cout << "Client Recvr Started on Port: " << itoa(args->port,Buffer, 10) << endl;
	try {
				
		SocketClient s = SocketClient(args->ipAddress, args->port);


		int length = 0;
		cout << "Connection accepted!" << endl;
		while(true) {
			string info = s.ReceiveLine();
			vector<string> splitInfo = vector<string>();
			Utils::SplitString(info, ",", splitInfo,false);
			//info = text.c_str();
			int cmd = atof(splitInfo[0].c_str());
			if (cmd == 0)
				continue;

			//cout << "CMD: " << cmd<< endl;

			int id;
			float mass, rad;
			float x, y, z;
			Vec3f loc, force;

			//cout << "New packet arrived!" << endl;

			int ptrCnt = 0;
			while (ptrCnt < (splitInfo.size()-1)) {

				cmd = atof(splitInfo[ptrCnt].c_str());
				//cout << "CMD: " << cmd<< endl;

				//decide what we want to do based on the command
				switch (cmd) {

				case INIT_ORBITAL_BODY:
					//get the gravbody information

					//cout << "\nAdd Body: " << 
					id = atof(splitInfo[++ptrCnt].c_str());
					x = atof(splitInfo[++ptrCnt].c_str());
					y = atof(splitInfo[++ptrCnt].c_str());
					z = atof(splitInfo[++ptrCnt].c_str());
					loc = Vec3f(x, y, z);
					mass = atof(splitInfo[++ptrCnt].c_str());
					rad = atof(splitInfo[++ptrCnt].c_str());
					if (!args->gameSim->addOrbitalBody(id, loc, rad, mass))
						cout << "\nCould not add o body!!\n";
					break;

				case INIT_DEBRIS_BODY:
					//get the gravbody information
					if(id % 50 == 0)
					{
						cout << "\nAdd Debris: " << (id = atof(splitInfo[++ptrCnt].c_str()));
					}
					//cout << "\nAdd Debris: " << 
					id = atof(splitInfo[++ptrCnt].c_str());
					x = atof(splitInfo[++ptrCnt].c_str());
					y = atof(splitInfo[++ptrCnt].c_str());
					z = atof(splitInfo[++ptrCnt].c_str());
					loc = Vec3f(x, y, z);
					mass = atof(splitInfo[++ptrCnt].c_str());
					rad = atof(splitInfo[++ptrCnt].c_str());
					if (!args->gameSim->addDebrisBody(id, loc, rad, mass))
						cout << "\nCould not add d body!!\n";
					break;

				case UPDATE_ORBITAL_BODY:
					try {
					id = atof(splitInfo[++ptrCnt].c_str());
					x = atof(splitInfo[++ptrCnt].c_str());
					y = atof(splitInfo[++ptrCnt].c_str());
					z = atof(splitInfo[++ptrCnt].c_str());
					loc = Vec3f(x, y, z);
					x = atof(splitInfo[++ptrCnt].c_str());
					y = atof(splitInfo[++ptrCnt].c_str());
					z = atof(splitInfo[++ptrCnt].c_str());
					force = Vec3f(x, y, z);
					args->gameSim->updateOrbitalBody(id, &loc, &force);
					}catch ( string str ) { cerr << str << endl; }
					catch ( ... ) { cerr << "unhandled exception\n"; }
					break;

				case UPDATE_DEBRIS_BODY:					
					try{
					id = atof(splitInfo[++ptrCnt].c_str());
					x = atof(splitInfo[++ptrCnt].c_str());
					y = atof(splitInfo[++ptrCnt].c_str());
					z = atof(splitInfo[++ptrCnt].c_str());
					loc = Vec3f(x, y, z);
					args->gameSim->updateDebrisBody(id, &loc);
					}catch ( string str ) { cerr << str << endl; }
					catch ( ... ) { cerr << "unhandled exception\n"; }
					break;

				case CLIENT_SIM_START:
					args->gameSim->thinSimStart();
					break;

				}

				++ptrCnt;
			}
		}
		s.Close();			
	}
	catch ( string str ) { cerr << str << endl; }
	catch ( ... ) { cerr << "unhandled exception\n"; }
}